package com.wechet.controller;

import com.wechet.VO.ResultVo;
import com.wechet.dto.OrderDto;
import com.wechet.exception.SellException;
import com.wechet.serveice.BuyerService;
import com.wechet.serveice.impl.orderServiceImpl;
import com.wechet.utils.OrderForm2OrderdDTO;
import com.wechet.utils.ResultVoUtil;
import com.wechet.form.OrderForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sc on 2017/9/23.
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private orderServiceImpl orderService;
    @Autowired
    private BuyerService buyerService;

    //创建
    @PostMapping("/create")
    public ResultVo<Map<String,String>> create(@Valid OrderForm orderForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("创建订单出错{},{}",orderForm,bindingResult.getFieldError().getDefaultMessage());
            throw new SellException(bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDto  orderDto = OrderForm2OrderdDTO.convert(orderForm);
       if(CollectionUtils.isEmpty(orderDto.getOrderDetailList())){
           throw new SellException("购物车为空");
       }
        OrderDto result =   orderService.create(orderDto);
        Map<String,String> map =  new HashMap<>();
        map.put("orderId",result.getOrderId());
        return ResultVoUtil.success(map);
    }
    //订单列表
    @GetMapping("/list")
    public ResultVo<List<OrderDto>> listAll(@RequestParam("openid") String openid,
                                            @RequestParam(value = "page",defaultValue = "0") Integer page,
                                            @RequestParam(value = "size",defaultValue = "10") Integer size){

            if(StringUtils.isEmpty(openid)){
                throw  new SellException("请先登录");
            }
        PageRequest pageRequest =  new PageRequest(page,size);
        Page<OrderDto> orderDtoPage = orderService.findList(openid,pageRequest);

        //Date ->long
        return ResultVoUtil.success(orderDtoPage.getContent());
    }

    //订单详情
    @GetMapping("/detail")
    public ResultVo<OrderDto> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderid") String orderid){

       OrderDto  order = buyerService.findOrderOne(openid, orderid);
       return ResultVoUtil.success(order);
    }

    //取消订单
    @PostMapping("/cancel")
    public ResultVo cancelOrder(@RequestParam("openid") String openid,
                                @RequestParam("orderid") String orderid){
        buyerService.cancelOrder(openid, orderid);

        return ResultVoUtil.success();
    }



}
